package servlets;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServlet;

import dao.NewspaperDAO;
import dao.hibernate.NewspaperDAOHibernateImpl;

import model.Newspaper;
import newspapers.autopublishing.AutoNewspaper;
import newspapers.autopublishing.NewspaperConfig;
import newspapers.autopublishing.TimeSeg;

/**
 * Servlet implementation class InitServlet.
 */
public class InitServlet extends HttpServlet {
    /** Serial version UID. */
    private static final long serialVersionUID = 1L;

    /** DAO to modify newspapers in the database. */
    private NewspaperDAO newspaperDAO;

    /**
     * Create and initialize newspapers.
     * (and tag clouder when it works)
     *
     * @param sc  servlet config
     * @see HttpServlet#HttpServlet()
     */
    @Override
    public void init(final ServletConfig sc) {
//        super(sc);

        //TODO: REMOVE: this is only here because it's the 1st method
        //to be executed

        newspaperDAO = new NewspaperDAOHibernateImpl();

        //get the newspapers from the database
        List<Newspaper> nps = newspaperDAO.getNewspapers();

        //let the two last elements in the list be autopublishers
        final int numAuto = 2;

        //don't initialize autonewspapers yet
        for (int i = 0; i < nps.size() - numAuto; i++) {
            Newspaper np = nps.get(i);
            //get the brokers to publish to
                np.requestBroker();
        }

        //for autopublishers
        ArrayList<NewspaperConfig> publishers
                = new ArrayList<NewspaperConfig>();

        NewspaperConfig pubConfig1 = new NewspaperConfig();
        pubConfig1.username = nps.get(nps.size() - 1).getUsername();
        pubConfig1.password = nps.get(nps.size() - 1).getPassword();
        pubConfig1.name = nps.get(nps.size() - 1).getName();
        pubConfig1.topicPrefix = nps.get(nps.size() - 1).getTopicPrefix();
        pubConfig1.topics = nps.get(nps.size() - 1).getTopics();
        TimeSeg ts11 = new TimeSeg();
        ts11.tId = 11;
        ts11.timeStart = 1;
        ts11.timeStop = 1000;
        ts11.distribution = "exp";
        ts11.messageLengthDistribution = "uni";
        ts11.messageLength = 10;
        ts11.mldStdDeviation = 2;
        ts11.ingressNode = "root";
        ts11.meanRate = 3;
        ts11.minRate = 1;
        ts11.maxRate = 5;
        ts11.useMeanRate = false;
        ts11.topicName = nps.get(nps.size() - 1).getTopicPrefix() + ".sport";
        TimeSeg ts12 = new TimeSeg();
        ts12.tId = 12;
        ts12.timeStart = 1001;
        ts12.timeStop = 1200;
        ts12.distribution = "exp";
        ts12.messageLengthDistribution = "uni";
        ts12.messageLength = 50;
        ts12.mldStdDeviation = 1;
        ts12.ingressNode = "root";
        ts12.meanRate = 2;
        ts12.minRate = 1;
        ts12.maxRate = 3;
        ts12.useMeanRate = false;
        ts12.topicName = nps.get(nps.size() - 1).getTopicPrefix() + ".music";
        pubConfig1.tsegs.add(ts11);
        pubConfig1.tsegs.add(ts12);
        publishers.add(pubConfig1);

        NewspaperConfig pubConfig2 = new NewspaperConfig();
        pubConfig2.username = nps.get(nps.size() - 2).getUsername();
        pubConfig2.password = nps.get(nps.size() - 2).getPassword();
        pubConfig2.name = nps.get(nps.size() - 2).getName();
        pubConfig2.topicPrefix = nps.get(nps.size() - 2).getTopicPrefix();
        pubConfig2.topics = nps.get(nps.size() - 2).getTopics();
        TimeSeg ts21 = new TimeSeg();
        ts21.tId = 11;
        ts21.timeStart = 1;
        ts21.timeStop = 1000;
        ts21.distribution = "exp";
        ts21.messageLengthDistribution = "uni";
        ts21.messageLength = 20;
        ts21.mldStdDeviation = 0.5;
        ts21.ingressNode = "root";
        ts21.meanRate = 3;
        ts21.minRate = 1;
        ts21.maxRate = 5;
        ts21.useMeanRate = false;
        ts21.topicName = nps.get(nps.size() - 2).getTopicPrefix() + ".sport";
        TimeSeg ts22 = new TimeSeg();
        ts22.tId = 12;
        ts22.timeStart = 1001;
        ts22.timeStop = 1200;
        ts22.distribution = "exp";
        ts22.messageLengthDistribution = "uni";
        ts22.messageLength = 65;
        ts22.mldStdDeviation = 3;
        ts22.ingressNode = "root";
        ts22.meanRate = 2;
        ts22.minRate = 1;
        ts22.maxRate = 3;
        ts22.useMeanRate = false;
        ts22.topicName = nps.get(nps.size() - 2).getTopicPrefix() + ".arts";
        pubConfig2.tsegs.add(ts21);
        pubConfig2.tsegs.add(ts22);
        publishers.add(pubConfig2);

        AutoNewspaper[] newspapers = new AutoNewspaper[2];

        for (int i = 0; i < 2; i++) {
            //create autonewspaper
            newspapers[i] = nps.get(nps.size() - (i + 1)).toAutoNewspaper(
                    publishers.get(i).tsegs.get(0));
            //get the brokers to publish to
            newspapers[i].requestBroker();
            //change the newspaper for an autonewspaper
            nps.set(nps.size() - (i + 1), newspapers[i]);
        }

        //Put the newspapers in a HashMap
        HashMap<String, Newspaper> npList = new HashMap<String, Newspaper>();
        for (Newspaper np : nps) {
            npList.put(np.getClientId(), np);
        }
        //Add them to the app context
        sc.getServletContext().setAttribute("npList", npList);
        //Add "publishers" and "newspapers" to the app context
        //for client control
        sc.getServletContext().setAttribute("pubCC", publishers);
        sc.getServletContext().setAttribute("npCC", newspapers);

//        //Create a tag clouder
//        TagClouder tagClouder = new TagClouder("Tag Clouder", "guest", "guest");
//        sc.getServletContext().setAttribute("tagClouder", tagClouder);

    }

//    /**
//     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
//     */
//    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//    }
//
//    /**
//     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
//     */
//    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//    }

}
